package com.example.himalayaproject.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.himalayaproject.R;
import com.example.himalayaproject.subscription.fragment.SubHistoryFragment;
import com.example.himalayaproject.subscription.fragment.SubRecommendFragment;
import com.example.himalayaproject.adapter.FindViewpagerAdapter;
import com.example.himalayaproject.subscription.fragment.SubScriptionFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScriptionFragment extends Fragment implements SubScriptionFragment.onIntentRecommendListent {
  private   ViewPager pager;
    public ScriptionFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_subscription, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        aboutTabLayout(view);
    }

    private void aboutTabLayout(View view) {
        pager = (ViewPager) view.findViewById(R.id.subscription_fragment_viewpager);
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.subscription_fragment_tablayout);
        List<String> list = new ArrayList<>();
        list.add("推荐");
        list.add("订阅");
        list.add("历史");

        List<Fragment> fragments = new ArrayList<>();
        SubRecommendFragment subRecommendFragment = new SubRecommendFragment();
        SubScriptionFragment subScriptionFragment = new SubScriptionFragment();
        SubHistoryFragment subHistoryFragment = new SubHistoryFragment();
        fragments.add(subRecommendFragment);
        fragments.add(subScriptionFragment);
        fragments.add(subHistoryFragment);
        FindViewpagerAdapter adapter = new FindViewpagerAdapter(getChildFragmentManager(),list,fragments);
        pager.setOffscreenPageLimit(3);
        pager.setAdapter(adapter);
        tabLayout.setupWithViewPager(pager);
        pager.setCurrentItem(1);
        subScriptionFragment.setListener(this);
    }

    @Override
    public void onIntentRecommend() {
        pager.setCurrentItem(0);
    }
}
